//
//  OnePageViewController.m
//  Teacher
//
//  Created by cong on 15/11/12.
//  Copyright © 2015年 liangdianxiong. All rights reserved.
//

#import "OnePageViewController.h"
#import "Cell_1.h"
#import "Cell_2.h"
#import "Cell_3.h"
#import "Cell_4.h"
#import "CVOnePage.h"
#import "SearchController.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)




@interface OnePageViewController ()<UIScrollViewDelegate>


@property (strong, nonatomic) IBOutlet CommonTableView *tv_table;

@property (strong,nonatomic) UIScrollView *sv_image;
@property (strong,nonatomic) UIScrollView *sv_title;
@property (strong,nonatomic) UIView *v_bg_image;

//广告图片
@property(nonatomic,strong)NSArray *imageNames;
@property(nonatomic,strong)UIPageControl *pageControl;
@end

@implementation OnePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton * btnLeft = [UIButton buttonWithType:UIButtonTypeSystem];
    btnLeft.frame =CGRectMake(0, 0, 60, 44);

    [btnLeft setTitle:@"搜索" forState:UIControlStateNormal];
//    [btnLeft mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(v_bg.mas_right).with.offset(10);
//        make.centerY.equalTo(v_bg);
//    }];
    
    [btnLeft.titleLabel sizeToFit];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"用户" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftItem:)];
    

    self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc]initWithCustomView:btnLeft];//[[UIBarButtonItem alloc]initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftItem:)];
    
    
    
    UITextField *tf_chazhao=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-100, 30)];
    tf_chazhao.backgroundColor=[UIColor whiteColor];


    [[tf_chazhao rac_signalForSelector:@selector(becomeFirstResponder)] subscribeNext:^(RACTuple * tuple) {
        
        NSLog(@"输入------");
        [tf_chazhao resignFirstResponder];
        SearchController * searchController = [self.storyboard instantiateViewControllerWithIdentifier:@"SearchController"];
        [self.navigationController pushViewController:searchController animated:YES];
        
    }];
    
    self.navigationItem.titleView=tf_chazhao;
    
    
    UIView * headerView = [[UIView alloc]init];
    
    _sv_image = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,100)] ;
    
    _sv_image.contentSize=CGSizeMake(_sv_image.frame.size.width*self.imageNames.count, _sv_image.frame.size.height);
    
    //设置ScrollView滚动内容的区域
    NSMutableArray * arrIvs = [@[]mutableCopy];
    

    for (int i=0;i<self.imageNames.count;i++){
        UIImageView *image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:self.imageNames[i]]];
        image.contentMode = UIViewContentModeScaleAspectFill;
        
         [_sv_image addSubview:image];
        
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(_sv_image);
        }];
        
        
        [arrIvs addObject:image];
    }

    [_sv_image distributeSpacingHorizontallyWith:arrIvs];
    
    //开启滚动分页功能，如果不需要这个功能关闭即可
    [_sv_image setPagingEnabled:YES];
    
    //隐藏横向与纵向的滚动条
    [_sv_image setShowsVerticalScrollIndicator:NO];
    [_sv_image setShowsHorizontalScrollIndicator:NO];
    
    //在本类中代理scrollView的整体事件
    [_sv_image setDelegate:self];
    
    
    
    //整体再将ScrollerView显示在窗口中
    [self.view addSubview:_sv_image];
    
    //页面控制小工具
    //它会在底部绘制小圆点标志当前显示页面
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _sv_image.frame.size.height -20,self.view.frame.size.width, 20)];
    //设置页面的数量
    [_pageControl setNumberOfPages:4];
    //监听页面是否发生改变
     _pageControl.pageIndicatorTintColor=[UIColor redColor];
    
    headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 100 + 165);
    
    
    [headerView addSubview:_sv_image];
    [headerView addSubview:_pageControl];
  _tv_table.tableHeaderView = headerView;
    
//导入xib文件
    CVOnePage * cvOnePage = [[[NSBundle mainBundle] loadNibNamed:@"CVOnePage" owner:self options:nil] firstObject];
    
     [headerView addSubview:cvOnePage];
    
    [cvOnePage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(headerView);
        make.top.equalTo(_sv_image.mas_bottom);
    }];
    
    
    NSArray * arrParams = @[@{@"title":@"我大苏打撒"},@{@"title":@"我发生的事"},@{@"title":@"你ADS"},@{@"title":@"他"},@{@"title":@"我as电话卡"},@{@"title":@"我大师傅看是否"},@{@"title":@"我大苏打"},@{@"title":@"我但是撒"}];
    
    for (int i=0; i<arrParams.count; i++) {
        

        [cvOnePage addNibWithEntity:arrParams[i] andCellName:@"CCellOnePage"];
    }
    
    _tv_table.arr_section = @[@(20),@(20),@(20),@(20)];//section相隔距离
    
    [_tv_table addNibWithEntity:@{} andCellName:@"Cell_1" andSection:0];
    [_tv_table addNibWithEntity:@{} andCellName:@"Cell_2" andSection:1];
    [_tv_table addNibWithEntity:@{} andCellName:@"Cell_3" andSection:2];
   
   
    
    for (int i=0; i<20; i++) {
        [_tv_table addNibWithEntity:@{} andCellName:@"Cell_4" andSection:3];
    }
   
}

-(NSArray *)imageNames{
    if (!_imageNames) {
        _imageNames=@[@"1.jpg",@"5.png",@"5.png",@"5.png"];
    }
    return _imageNames;
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int index = scrollView.contentOffset.x/SCREEN_WIDTH;
    NSLog(@"-----%d----%f",index,scrollView.contentOffset.x);
    _pageControl.currentPage = index;
}





- (void)clickLeftItem:(id)sender {
	
}



@end
